package com.hsck.order.kds.main.dialog

import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.hsck.order.kds.R
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.SelectQuantityDialogLayoutBinding

/**
 * 标准划菜选择份数
 */
class SelectQuantityDialog(context: Activity) : Dialog(context, R.style.Dialog_FullScreen) {
    private lateinit var onCliclLisener: SelectQuantityListener
    private val activity: Activity = context

    private var binding: SelectQuantityDialogLayoutBinding = DataBindingUtil.inflate(
        LayoutInflater.from(context),
        R.layout.select_quantity_dialog_layout,
        null,
        false
    )

    init {
        createDialog()
    }

    private fun createDialog() {
        setContentView(binding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        val lp = window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT

        lp?.alpha = 1f
        window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        binding.viewClose.setOnSingleClickListener { dismiss() }
        binding.tvOne.setOnSingleClickListener {
            dismiss()
            onCliclLisener.onSelectQuantity(1)
        }
        binding.tvTwo.setOnSingleClickListener {
            dismiss()
            onCliclLisener.onSelectQuantity(2)
        }
    }

    fun show(dishName: String) {
        show()
        binding.tvDishName.text = dishName
    }

    fun setSelectQuantityListener(clickListener: SelectQuantityListener) {
        this.onCliclLisener = clickListener
    }

    interface SelectQuantityListener {
        fun onSelectQuantity(i: Int)
    }
}
